package com.youlai.boot.diet.service.impl;

import com.youlai.boot.common.enums.WeekEnum;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.common.model.WeekCalendarVO;
import com.youlai.boot.energyPlan.model.form.UserEnergyPlanForm;
import com.youlai.boot.energyPlan.service.UserEnergyPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.diet.mapper.DietPlanMapper;
import com.youlai.boot.diet.service.DietPlanService;
import com.youlai.boot.diet.model.entity.DietPlan;
import com.youlai.boot.diet.model.form.DietPlanForm;
import com.youlai.boot.diet.model.query.DietPlanQuery;
import com.youlai.boot.diet.model.vo.DietPlanVO;
import com.youlai.boot.diet.converter.DietPlanConverter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 饮食计划服务实现类
 *
 * @author cuisheng
 * @since 2025-03-30 19:06
 */
@Service
@RequiredArgsConstructor
public class DietPlanServiceImpl extends ServiceImpl<DietPlanMapper, DietPlan> implements DietPlanService {

    private final DietPlanConverter dietPlanConverter;
    private final UserEnergyPlanService energyPlanService;

    /**
    * 获取饮食计划分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<DietPlanVO>} 饮食计划分页列表
    */
    @Override
    public IPage<DietPlanVO> getDietPlanPage(DietPlanQuery queryParams) {
        Page<DietPlanVO> pageVO = this.baseMapper.getDietPlanPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取饮食计划表单数据
     *
     * @param id 饮食计划ID
     * @return
     */
    @Override
    public DietPlanForm getDietPlanFormData(Long id) {
        DietPlan entity = this.getById(id);
        return dietPlanConverter.toForm(entity);
    }
    
    /**
     * 新增饮食计划
     *
     * @param formData 饮食计划表单对象
     * @return
     */
    @Override
    public boolean saveDietPlan(DietPlanForm formData) {
        DietPlan entity = dietPlanConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新饮食计划
     *
     * @param id   饮食计划ID
     * @param formData 饮食计划表单对象
     * @return
     */
    @Override
    public boolean updateDietPlan(Long id,DietPlanForm formData) {
        DietPlan entity = dietPlanConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除饮食计划
     *
     * @param ids 饮食计划ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteDietPlans(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的饮食计划数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<WeekCalendarVO> weekCalendar(List<String> dates) {
        Long userId = SecurityUtils.getUserId();
        UserEnergyPlanForm planFormData = energyPlanService.getUserEnergyPlanFormData();
        Integer limit=0;
        if (planFormData != null) {
            limit=planFormData.getEnergy();
        }
        Map<Integer,String> dietPlanWeekCalendarVOMap=new HashMap<>();
        for (WeekEnum weekEnum : WeekEnum.values()) {
            dietPlanWeekCalendarVOMap.put(weekEnum.getValue(),weekEnum.getLabel());
        }

        List<WeekCalendarVO> weekCalendarVOS = baseMapper.weekCalendar(userId, dates);
        if (weekCalendarVOS != null&&!weekCalendarVOS.isEmpty()) {
            for (WeekCalendarVO vo : weekCalendarVOS) {
                Double energy = vo.getEnergy();
                vo.setWeek(dietPlanWeekCalendarVOMap.get(Integer.parseInt(vo.getWeek())));
                vo.setIsBeyond(energy>limit);
            }
        }
        return weekCalendarVOS;
    }

}
